<?php
class Login extends CI_Controller
{
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');
	}
	
	public function index(){
		if(!$this->input->post('username')){
			$data['title'] = '登录';
			$this->load->view('admin/header', $data);
			$this->load->view('admin/login', $data);
			$this->load->view('admin/footer', $data);
		}
		else{
			$this->db->select('username, id');
			$q = $this->db->get_where('admin', array('username'=>$this->input->post('username'), 'password'=>$this->input->post('password')));			
			if($q->result()){
				$now = date("Y-m-d H:i:s");
				$u = $q->result();
				$this->session->set_userdata($u[0]);
				$setarr = array(
					'lastloginip'	=> $_SERVER['REMOTE_ADDR'],
					'lastlogintime'	=> $now
				);
				
				$this->db->where('id', $u[0]->id);
				$this->db->update('admin', $setarr);
				
				// 记录登录历史
				$setarr = array(
					'adminid'=>$u[0]->id,
					'username'=>$u[0]->username,
					'loginip'=>$_SERVER['REMOTE_ADDR'],
					'createtime'=>$now
				);
				$this->db->insert('adminloginlog',$setarr);
//				$sess = $this->session->all_userdata();
//				print_r($sess);
//				die();
				redirect(site_url('sysadmin/home'), 'refresh');
			}else{
				redirect(site_url('sysadmin/login'), 'refresh');
			}
			
			if($this->input->post('username') == 'chang'){
				echo 'great';
			}
		}
	}
}